<?php
require __DIR__ . '/../vendor/autoload.php';

use Swoole\OpenAi\OpenAi;

$env = parse_ini_file('.env');
//var_dump($env['API_KEY']);exit();

$open_ai = new OpenAi($env['API_KEY']);
$open_ai->setBaseURL('https://chat.swoole.com');
//$messages[] = ["role" => "system", "content" => "You are a helpful assistant."];
//$messages[] = ["role" => "user", "content" => "Who won the world series in 2020?"];
//$messages[] = ["role" => "assistant", "content" => "The Los Angeles Dodgers won the World Series in 2020."];
//$messages[] = ["role" => "user", "content" => "Where was it played?"];
$messages[] = ["role" => "user", "content" => "你是谁呀"];
$txt = $error = '';

$complete = $open_ai->chat([
//    'model' => 'gpt-3.5-turbo',
    'model' => 'qwen-v1',
    'messages' => $messages,
    'temperature' => 1.0,
    //    'stream' => true,
], function ($curl_info, $data) use (&$txt, &$error) {
    // 请求结束
    if ($data === '[DONE]') {
        return;
    }
    $chunk = json_decode($data, true);
    var_dump($chunk);
    file_put_contents('res.json', json_encode($chunk, 320));
    // 发生了错误
    if (is_array($chunk)) {
        $error = $chunk;
    } else {
        $txt .= $chunk;
    }
});
var_dump($complete);
if ($complete) {
    var_dump($txt, $error);
} else {
    var_dump($open_ai->getError(), $open_ai->getErrno());
}

//sleep(2);
echo '==== OVER ====' . PHP_EOL;

